package com.shop.util;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * 
 * @author wenjuanhenxing
 * @version V1.0 MyBatisUtil.java 2016-2-10 上午11:17:40
 */
// 提供获取SqlSessionFactory对象或管理Sesison的操作,解耦合，方便复用
public class MyBatisUtil {
	public static final String CONFIG_PATH = "mybatis-config.xml";

	// 每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心
	// 使用SqlSessionFactory的最佳实践是在应用运行期间不要重复创建多次,最佳范围是应用范围
	private static SqlSessionFactory sqlSessionFactory = null;

	static {
		Reader read = null;
		try {
			read = Resources.getResourceAsReader(CONFIG_PATH);
		} catch (IOException e) {
			System.out.println("Read资源文件异常");
			e.printStackTrace();
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(read);
	}

	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}

	// 获取session
	public static SqlSession getSqlSession() {
		return sqlSessionFactory.openSession();
	}

	// 关闭session
	public static void closeSession(SqlSession sqlSession) {
		if (sqlSession != null) {
			sqlSession.close();
		}
	}

	/**
	 * 获取SqlSession
	 * 
	 * @param isAutoCommit
	 *            true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务 false
	 *            不会自动提交事务，这时就需要我们手动调用sqlSession. commit()提交事务
	 * @return SqlSession
	 */
	public static SqlSession getSqlSession(boolean isAutoCommit) {
		return sqlSessionFactory.openSession(isAutoCommit);
	}
}
